package com.xiangban.data_platform.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiangban.data_platform.domain.User;
import com.xiangban.data_platform.domain.dto.*;
import com.xiangban.data_platform.mapper.QuenstionnaireMapper;
import com.xiangban.data_platform.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author Mr. Ma
 * @Data 2020/12/2 12:51
 */
@Service
public class QuenstionnaireService {
    @Autowired
    private QuenstionnaireMapper quenstionnaireMapper;
    @Autowired
    private UserMapper userMapper;



    public List<Quenstions> findQuenstionAnswer(){

        List<Quenstions> quenstionAnswer = quenstionnaireMapper.findquenstionAnswer();
        return quenstionAnswer;
    }


    public Object findBeChosen(int userId){

        List<Quenstionnaire> beChosen = quenstionnaireMapper.findBeChosen(userId);
        if(beChosen !=null){
            return beChosen;
        }
        return null;
    }


    public void saveQuenstionnaire(Map<String,Object> map){
        List<Object> quenstion_answer = (List<Object>) map.get("quenstion_answer");
        ObjectMapper objectMapper = new ObjectMapper();
        Object user = map.get("User");
        User user1 = objectMapper.convertValue(user, User.class);
        userMapper.updateUser(user1);
        quenstionnaireMapper.del(user1.getId());
        for (Object o : quenstion_answer) {
            Quenstionnaire quenstionnaire = objectMapper.convertValue(o, Quenstionnaire.class);
            quenstionnaireMapper.saveQuenstionnaire(quenstionnaire);
        }
    }
    public List selectAnswer(Answer answer){
        List answers = quenstionnaireMapper.selectAnswer(answer);
        return answers;
    }

    public Object selectAnswerById(AnswerByAnswerId answer){
        List<Answer> answers = quenstionnaireMapper.selectAnswerById(answer);
        String str="";
        for (Answer answerVo : answers) {
            str+=answerVo.getAnswer()+",";
        }
        return str.substring(0, str.length()-1);
    }
    public int saveUserQuenstionnaire(Map<String,Object> map){
        List<Object> quenstion_answer = (List<Object>) map.get("quenstion_answer");
        ObjectMapper objectMapper = new ObjectMapper();
        Object user = map.get("User");
        User user1 = objectMapper.convertValue(user, User.class);
        User byphone = userMapper.findByPhone(user1);//通过手机号和平台查询出的sql语句。
        if (byphone!=null ){
            return -1;
        }
        userMapper.saveUser(user1);
        quenstionnaireMapper.del(user1.getId());
        for (Object o : quenstion_answer) {
            Quenstionnaire quenstionnaire = objectMapper.convertValue(o, Quenstionnaire.class);
            quenstionnaire.setUserId(user1.getId());
            quenstionnaireMapper.saveQuenstionnaire(quenstionnaire);
        }
        return 1;
    }

    public int updateAnswer(Answer answer) {
        int i = quenstionnaireMapper.updateAnswer(answer);
        return i;
    }

    public int saveAnswer(Answer answer) {
        int i= quenstionnaireMapper.saveAnswer(answer);
        return i;
    }

    public int deleteAnswer(Answer answer) {
        int i= quenstionnaireMapper.deleteAnswer(answer.getId());
        return i;
    }

    public Answer findAnswerById(Answer answer) {

        return  quenstionnaireMapper.ByIdGetAnswer(answer.getId());

    }
}
